<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class LoginAction extends BaseAccountAction {

	//登录验证
	public function index() {
		if ($_SERVER["REQUEST_METHOD"] != "POST")
			return false;
		$account = strtolower(trim($_POST["account"]));
		$password = $_POST["password"];
		if (empty($account))
			$this->ajaxReturn(null, array('account' => "请填写账号"), -1);
		if (empty($password))
			$this->ajaxReturn(null, array('password' => "请填写密码"), -2);
		if (Model::regex($account, "email"))
			$User = D("User")->where("email='$account'");
		elseif (preg_match('/^[\d]{11}$/', $account))
			$User = D("User")->where("mobilephone='$account'");
		else
			$this->ajaxReturn(null, array('account' => "账号格式有误，请输入Email或手机号码"), -3);
		$ret = $User->find();
		if ($ret) {
			if (md5($password) == $ret["password"]) {
				//登录成功
				Session::set("user_id", $ret["user_id"]);
				if ($ret["school_id"] == 0) {
					
				} else {
					Session::set("school_id", $ret["school_id"]);
				}
				$this->ajaxReturn(__ROOT__ . "/", "登陆成功", 1);
			} else {
				$this->ajaxReturn(null, array('password' => "密码输入有误"), -4);
			}
		} else {
			$this->ajaxReturn(null, array('account' => "账号输入有误"), -5);
		}
	}

	//退出
	public function logout() {
		Session::clear();
		redirect(C("SITE_URL"));
	}

}

?>
